<template>
	<view>
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			title="单店销售报表" @clickLeft="back" />
		<uni-notice-bar show-icon scrollable style="height: 15px;" 
				:speed="50" text="注意:连锁店用户在9:00~22:00只能查询间隔6个月范围内数据信息!" />

		<uni-forms :modelValue="baseFormData" ref="form" style="margin-left: 10px;" :rules="rules">
			<!-- <uni-forms-item label="连锁店:" style="margin-top: 15px;" name="chainStore">
				<uni-data-select style="width: 75%;  margin-left: -10px; " v-model="baseFormData.chainStore" 
					@change="changeStore" placeholder="请选择连锁店"  :localdata="store"></uni-data-select>
			</uni-forms-item> -->
			
			<uni-forms-item label="连锁店:" style="margin-top: 15px; width: 70%;" name="chainStore">
					<uni-combox style="width: 95%;  margin-left: -10px;"  :candidates="candidates" placeholder="所有连锁店" v-model="chain"  @input="getHandle(chain)"></uni-combox>
			</uni-forms-item>
			
			 <button type="warn" size="mini" @click="clearInput" style="position: relative; left: 75%; top: -55px;">清空</button>
			
			<uni-forms-item label="经手人:" style="margin-top: -40px;" name="handledBy">
				<uni-data-select style="width: 50%;  margin-left: -10px; " v-model="baseFormData.handledBy"
					placeholder="所有经手人" :localdata="handler"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="日期:" style="margin-top: -5px;" label-width="80" >
				<view class="example-body" style="width: 38%;">
					<uni-datetime-picker type="date" :clear-icon="false" v-model="baseFormData.startTime"  />
				</view>						
				<uni-text style="position: relative; left: 42%; top: -50rpx;">至</uni-text>
				<view class="example-body" style="width: 38%; position: absolute; left: 50%; top: 0px;">
					<uni-datetime-picker type="date" :clear-icon="false" v-model="baseFormData.endTime"  />
				</view>
			</uni-forms-item>
			<view>
				<button size="mini" type="primary" style="top: -30px;" @click="submit">查询报表</button>
			</view>

		</uni-forms>


		<view  class="uni-container" style="margin-top: -25px;">
			<uni-table ref="table" :loading="loading" border stripe emptyText="">
				<uni-tr>
					<uni-th width="10" align="center">销售总量</uni-th>
					<uni-th width="20" align="center">退货总量</uni-th>
					<uni-th width="20" align="center">净销售量</uni-th>
					<uni-th width="20" align="center">赠品数量</uni-th>
				</uni-tr>
				<uni-tr style="height: 30px;">
					<uni-td align="center">{{ tableData.totalQuantity }}</uni-td>
					<uni-td align="center">{{ tableData.totalQuantityR }}</uni-td>
					<uni-td align="center">{{ tableData.netSales }}</uni-td>
					<uni-td align="center">{{ tableData.totalQuantityF }}</uni-td>
				</uni-tr>

				<uni-tr>
					<uni-th width="20" align="center">销售额</uni-th>
					<uni-th width="20" align="center">退货额</uni-th>
					<uni-th width="20" align="center">净售额</uni-th>
					<uni-th width="20" align="center">利润</uni-th>
				</uni-tr>
				<uni-tr style="height: 30px;">
					<uni-td align="center">{{ tableData.netAmount }}</uni-td>
					<uni-td align="center">{{ tableData.netAmountR }}</uni-td>
					<uni-td align="center">{{ tableData.inNetAmount }}</uni-td>
					<uni-td align="center">{{ tableData.netSalesProfit }}</uni-td>
				</uni-tr>	
				<uni-tr>
					<uni-th width="20" align="center">优惠</uni-th>
					<uni-th width="20" align="center">代金券</uni-th>
					<uni-th width="20" align="center">积分换现金</uni-th>
					<uni-th width="20" align="center">预存消费</uni-th>
				</uni-tr>
				<uni-tr style="height: 30px;">
					<uni-td align="center">{{ tableData.totalDiscountAmount }}</uni-td>
					<uni-td align="center">{{ tableData.coupon }}</uni-td>
					<uni-td align="center">{{ tableData.vipScore }}</uni-td>
					<uni-td align="center">{{ tableData.chainPrepaidAmount }}</uni-td>
				</uni-tr>
				<uni-tr>
					<uni-th width="20" align="center">刷卡</uni-th>
					<uni-th width="20" align="center">现金</uni-th>
					<uni-th width="20" align="center">微信</uni-th>
					<uni-th width="20" align="center">支付宝</uni-th>
				</uni-tr>
				<uni-tr style="height: 30px;">
					<uni-td align="center">{{ tableData.cardAmount }}</uni-td>
					<uni-td align="center">{{ tableData.cashAmount }}</uni-td>
					<uni-td align="center">{{ tableData.weChatAmount }}</uni-td>
					<uni-td align="center">{{ tableData.alipayAmount }}</uni-td>
				</uni-tr>
				<uni-tr>
					<uni-th width="20" align="center">预存刷卡</uni-th>
					<uni-th width="20" align="center">预存现金</uni-th>
					<uni-th width="20" align="center">预存微信</uni-th>
					<uni-th width="20" align="center">预存支付宝</uni-th>
				</uni-tr>
				<uni-tr style="height: 30px;">
					<uni-td align="center">{{ tableData.prestoreCard }}</uni-td>
					<uni-td align="center">{{ tableData.prestoreCash }}</uni-td>
					<uni-td align="center">{{ tableData.prestoreWeChat }}</uni-td>
					<uni-td align="center">{{ tableData.prestoreAlipay }}</uni-td>
				</uni-tr>
				<uni-tr>
					<uni-th width="20" align="center">实收现金等</uni-th>
					<uni-th width="20" align="center"></uni-th>
					<uni-th width="20" align="center"></uni-th>
					<uni-th width="20" align="center"></uni-th>
				</uni-tr>
				<uni-tr>
					<uni-th width="20" align="center">{{tableData.netReceipts}}</uni-th>
					<uni-th width="20" align="center"></uni-th>
					<uni-th width="20" align="center"></uni-th>
					<uni-th width="20" align="center"></uni-th>
				</uni-tr>
			</uni-table>
		</view>

	</view>
</template>

<script>
	import {baseUrl} from 'libs/util/global'
	import getdateTime from '@/common/getdateTime.js'
	export default {
		data() {
			return {
				
				candidates: [],
				chain: '',
				// 表单数据
				baseFormData: {
					chainStore: '',
					handledBy: 0,
					startTime: '',
					endTime: ''	
				},
				store: [], // 连锁店下拉框
				handler: [], // 经手人下拉框
				chainId: '',				
				tableData: {},
				userId: '',
				isdministrators: false,
				// 表单校验
				rules: {
					chainStore: {
						rules: [{
							required: true,
							errorMessage: '请选择连锁店',
							ValidityState: 'submit'
						}, ]
					},
					documentTime: {
						rules: [{
							required: true,
							errorMessage: '请选择单据日期',
							ValidityState: 'submit'
						}, ]
					},
				},
				pageSize: 10,
				loginUserId: null,
				loginUserType: null,
				authority: null,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				userType: ''
			}
		},
		onLoad(option) {
			uni.getStorage({
				key: 'userId',
				success:(res)=> {
					// console.log(res.data + "登录用户id:"+res.data)
					this.loginUserId = res.data
				}
			});
			uni.getStorage({
				key: 'isChain',
				success:(res) =>{
					// console.log(res.data + "登录用户类型:"+res.data)
					this.loginUserType = res.data
				}
			});
			uni.getStorage({
				key: 'authority',
				success:(res) =>{
					// console.log(res.data + "登录用户权限:"+res.data)
					this.authority  = res.data
				}
			});
			// 获取当天时间
			var timeStr = getdateTime.dateTimeStr('y-m-d')
			this.baseFormData.startTime = timeStr
			this.baseFormData.endTime = timeStr
		},
		mounted() {
			this.getStorage()	
		},
		methods: {		
			clearInput(){
				this.chain = ''
				this.baseFormData.chainStore = null
			},
			getHandle(chain){
				console.log("查看chain:"+chain)				
				if(chain == ""){
					this.baseFormData.chainStore = null
				}else{
					console.log("进入判断")
					for (var i = 0; i < this.store.length; i++) {
						if(this.store[i].text === chain){
							console.log(this.store[i].value)
							this.baseFormData.chainStore = this.store[i].value
							this.changeStore(this.store[i].value)
						}
					}
				}
			},
			
			// 连锁店-经手人下拉框选中
			changeStore(e) {
				if(e == ''){
					console.log("店员下拉框"+e)
					this.handler = []
				}else{
					let url = baseUrl+'/chain/clerk/' + e
					uni.request({
						url: url,
						header: {
							'content-type': 'application/x-www-form-urlencoded'
						},
						method: 'GET',
						data: {},
						success: (res) => {						
							this.handler = res.data.data
						}
					})

				}
			},

			// 获取连锁店下拉框
			getStorage() {
				// 连锁店自营者 自己所拥有的店铺
				let url = baseUrl+'/chain/store/' +this.loginUserId + "/" + this.loginUserType
				uni.request({
					url: url,
					header: {'content-type': 'application/x-www-form-urlencoded'},
					method: 'GET',
					data: {},
					success: (res) => {
						this.store = res.data.data
						for (var i = 0; i < this.store.length; i++) {
							this.candidates.push(this.store[i].text) 
						}
					}
				})
			},

			//提交表单数据
			submit() {	
				this.tableData.totalQuantity = null
				this.tableData.totalQuantityR  = null
				this.tableData.netSales = null
				this.tableData.totalQuantityF = null
				this.tableData.netAmount = null
				this.tableData.netAmountR = null
				this.tableData.inNetAmount = null
				this.tableData.totalDiscountAmount = null
				this.tableData.netSalesProfit = null
				this.tableData.coupon = null
				this.tableData.vipScore = null
				this.tableData.chainPrepaidAmount = null
				this.tableData.cardAmount = null
				this.tableData.cashAmount = null
				this.tableData.weChatAmount = null
				this.tableData.alipayAmount = null
				this.tableData.prestoreCard = null
				this.tableData.prestoreCash = null
				this.tableData.prestoreWeChat = null
				this.tableData.prestoreAlipay = null
				this.tableData.netReceipts = null
				
				console.log("进入submit方法："+this.chain)
				
					
					this.$refs.form.validate().then(res => {  // 校验
					uni.showLoading({
					title: '加载中.....'
					})
					// 判断连锁店名称合法性
					if(this.candidates.indexOf(this.chain,0) >= 0){
						console.log("存在")
						// 判断时间
						let time = new Date()
						var startTime = new Date(this.baseFormData.startTime);
						var endTime = new Date(this.baseFormData.endTime);
						var timeDiff = endTime.getTime() - startTime.getTime() ;
						
						var dateDiff = timeDiff/(24 * 3600 * 1000) - 180;
						
						if(time.getHours() >= 9 && time.getHours() <= 22 && this.loginUserType == 1){  
							// console.log("允许通过的时间范围内")
							// 9:00 到 22：00 只能查询6个月内数据
							if(dateDiff < 0){
								// console.log("这里是允许通过的月份范围内")						
								uni.request({
									url: baseUrl+'/chain/orderSaler',
									header: {
										'content-type': 'application/json'
									},
									method: 'POST',
									data: {
										chainStore: this.baseFormData.chainStore,
										handledBy: this.baseFormData.handledBy,
										startTime: this.baseFormData.startTime,
										endTime: this.baseFormData.endTime
									},
									success: (res) => {
										setTimeout(function() {
											uni.hideLoading();
										}, 100);
										console.log(res)
										if (res.data.data != null) {
											if(this.authority == 2){
												res.data.data.netSalesProfit = '0.00'
												this.tableData = res.data.data
											}else{
												this.tableData = res.data.data
											}
																
										} else {										
											uni.showToast({
											title: '该时间段没有销售报表记录！',
											icon: 'none',
											duration: 500
												});
										}
									}
								})
							}else{
								uni.showToast({
								title: '9:00 - 22:00 只能能查最近6个月内数据',
								icon: 'none',
								duration: 1500
									});
							}
						}else{
							// 查询
							uni.request({
								url: baseUrl+'/chain/orderSaler',
								header: {
									'content-type': 'application/json'
								},
								method: 'POST',
								data: {
									chainStore: this.baseFormData.chainStore,
									handledBy: this.baseFormData.handledBy,
									startTime: this.baseFormData.startTime,
									endTime: this.baseFormData.endTime
								},
								success: (res) => {
									setTimeout(function() {
										uni.hideLoading();
									}, 100);
									console.log(res)
									if (res.data.data != null) {
										if(this.authority == 2){
											res.data.data.netSalesProfit = '0.00'
											this.tableData = res.data.data
										}else{
											this.tableData = res.data.data
										}
															
									} else {										
										uni.showToast({
										title: '该时间段没有销售报表记录！',
										icon: 'none',
										duration: 500
											});
									}
								}
							})
							}
						}else{
							console.log("不存在")
							uni.showToast({
							title: '请输入正确的连锁店名称!',
							icon: 'none',
							duration: 500
								});
						}
					
					
					
					
						
							}).catch(err => {
						
							})
				
				
				

					
					
					},
					// 左上角返回按钮
					back() {
						uni.switchTab({
							url: '/pages/work/work'
						})
					}
				}
				
			}
</script>

<style>
	/* #ifndef H5 */
	/* page {
	padding-top: 85px;
} */
	/* #endif */
	.uni-group {
		display: flex;
		align-items: center;
	}
</style>